import { TransformNode } from "@babylonjs/core/Meshes/transformNode";
import { Scene } from "@babylonjs/core/scene";
import { WebXRCamera } from "@babylonjs/core/XR/webXRCamera";
import { WebXRDefaultExperience } from "@babylonjs/core/XR/webXRDefaultExperience";
import { WebXRFeatureName } from "@babylonjs/core/XR/webXRFeaturesManager";
import { WebXRInput } from "@babylonjs/core/XR/webXRInput";
import { WebXRWalkingLocomotion } from '@babylonjs/core/XR/features/WebXRWalkingLocomotion';
import '@babylonjs/core/XR/features/WebXRWalkingLocomotion';


/**
 * 原地动作移动参数对象
 * @param xrInput 控制输入
 * @param locomotionTarget 动作对象
 */
interface IVRMotionLocomotionOptions {
    xrInput?: WebXRInput,
    locomotionTarget?: TransformNode | WebXRCamera,
}


/**
 * 原地动作移动
 * @param xr xr实例
 * @param scene 所在场景
 * @param options 参数对象
 */
class VRMotionLocomotion {

    /**
     * 原生原地动作移动组件
     */
    public locomotion: WebXRWalkingLocomotion;

    constructor(xr: WebXRDefaultExperience, scene: Scene, options?: IVRMotionLocomotionOptions) {
        const featureManager = xr.baseExperience.featuresManager;

        const ops = options || {};
        ops.xrInput = ops.xrInput || xr.input;
        ops.locomotionTarget = ops.locomotionTarget || this.setDefaultLocomotionTarget(scene, xr);

        this.locomotion = featureManager.enableFeature(WebXRFeatureName.WALKING_LOCOMOTION, 'latest', ops) as WebXRWalkingLocomotion;
    }

    /**
     * 设置默认动作对象
     * @param scene 所在场景
     * @param xr xr实例
     * @returns 动作节点对象
     */
    private setDefaultLocomotionTarget(scene: Scene, xr: WebXRDefaultExperience): TransformNode {
        const cameraParent = new TransformNode('webxr-camera-parent-node', scene);
        xr.baseExperience.camera.parent = cameraParent;
        return cameraParent;
    }

}


export { VRMotionLocomotion };
export type { IVRMotionLocomotionOptions };